package com.mingke.chat.message.logic;

import com.mingke.chat.config.RedisKeyEnum;
import com.mingke.chat.message.EasyChatMessage;
import com.mingke.chat.message.EasyChatMessageType;
import com.mingke.chat.message.request.EasyChatC2CRequest;
import com.mingke.chat.message.response.CommonChatResponse;
import com.mingke.chat.message.response.EasyChatC2CResponse;
import com.mingke.chat.message.response.EasyChatResponse;
import com.mingke.chat.server.EasyChatClientChannelTool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;

import java.text.SimpleDateFormat;

/**
 * @author mingke
 * @function TODO
 * @date 2021/12/20
 */
public class EasyChatC2CRequestLogic implements EasyChatMessageLogic{
    //日志框架
    private final Logger log = LoggerFactory.getLogger(this.getClass());
    //redis
    private RedisTemplate redisTemplate = null;
    //请求的响应
    private EasyChatC2CResponse response = new EasyChatC2CResponse();
    //日期格式化工具
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * 执行前准备的方法
     * 1. 消息的强制类型转化
     * 2. 检查用户是否在线
     */
    @Override
    public void prepare(EasyChatMessage message) {
        EasyChatC2CRequest easyChatC2CRequest = (EasyChatC2CRequest) message;
        BeanUtils.copyProperties(easyChatC2CRequest, response);
        log.info("收到来自: "+easyChatC2CRequest.getFromNumber()+"发送至: "+easyChatC2CRequest.getToNumber()+"的消息");
        Boolean isExist = redisTemplate.opsForHash().hasKey(RedisKeyEnum.UserONLine.getKeyName(), easyChatC2CRequest.getToNumber().toString());
        if(isExist) {
            Boolean isOnLine = (Boolean) redisTemplate.opsForHash().get(RedisKeyEnum.UserONLine.getKeyName(), easyChatC2CRequest.getToNumber().toString());
            if(isOnLine) {
                //用户在线，推送消息
                CommonChatResponse chatResponse = new CommonChatResponse(1, EasyChatMessageType.C2CResponse.getMessageCode(), new EasyChatResponse[]{response});
                EasyChatClientChannelTool.instance.writeToClient(easyChatC2CRequest.getToNumber(), chatResponse);
                log.info("消息投递成功");
                return;
            }
        }
        //用户不在线，无法投递
        log.info("用户不在线上，消息暂时无法投递");
        doStock(message);
    }

    /**
     * 数据存储的方法
     * 1. 消息离线存储到redis
     */
    @Override
    public void doStock(EasyChatMessage message) {
        EasyChatC2CRequest easyChatC2CRequest = (EasyChatC2CRequest) message;
        //离线消息使用List结构存储到redis中备用
        redisTemplate.opsForList().leftPush(easyChatC2CRequest.getToNumber().toString(), response);
    }

    /**
     * 消息分发的方法
     * 1. 获取到消息接收者的Channel进行数据发送
     */
    @Override
    public void dispatch(EasyChatMessage message) {

    }

    /**
     * 处理业务逻辑的方法
     */
    @Override
    public void doLogic(EasyChatMessage message) {
        prepare(message);
    }

    public EasyChatC2CRequestLogic() {
        this.redisTemplate = (RedisTemplate) EasyChatLogicContext.instance.getContext().getBean("redisTemplate");
    }
}
